package com.hns.can.energy.web;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.web.BaseController;
import com.hns.can.energy.service.DrvBehaviorAnalysisService;
import com.hns.can.energy.vo.NewEnergy;
import com.hns.can.master.entity.CommEnerConsStatInfoEntity;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;

/**
 * 驾驶行为分析
 * @author:zhangy
 * @email:zhangyu2222z@sina.cn
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/drvBehaviorAnalysis")
public class DrvBehaviorAnalysisController extends BaseController {
	
	@Autowired
    private DrvBehaviorAnalysisService drvBehaviorAnalysisService;
	
	private final String[] tableHeaderName={"驾驶员","车牌号","车辆编号","营运线路","所属机构","运营日期","运行时长","运行里程","运行能耗",
    		"百公里能耗(L|m³|KWh/100KM)","能耗排名",
    		"超速次数","急加速次数","急减速次数","ABS次数","空档滑行时长","空档滑行次数","经济车速时长"};
	private final String[] tablePropertyName = {"drvName","licPltNo","carInCd","lineName","organName","rcrdTime","totRngOprtDura",
    		"totRngMile","totRngEnerCons","enerConsPtg","enerRank",
    		"spedCnt","revupCount","sharpSldnCount","bhv7count","sctBhvDura","bhv5count",
    		"ecnSpdOfMotVhiDura"};
	
	@RequestMapping("/searchDrvBehaviorAnalysis")
	@ResponseBody
    public Object searchDeptEnergy(NewEnergy vo) throws Exception {
        if (vo.getOrgIds() == null || vo.getBeginTime() == null || vo.getEndTime() == null) {
            throw new BusinessException(MsgType.ERROR.getFlag(), "参数错误");
        }
        SysUserEntity user = (SysUserEntity) request.getSession().getAttribute("SES_CURRENTUSER");
        vo.setCorpId(user.getCorpId());
        vo.setDriverRelTableName("T_COMM_ENER_CONS_STAT_INFO");
        Pagination page = drvBehaviorAnalysisService.searchDrvBehaviorAnalysis(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
	
	/**
     * 
     * @param vo
     * @param request
     * @param response
     * @throws Exception
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2014-11-14
     * @功能说明：导出excel
     */                
    @RequestMapping("/exportDrvBehaviorAnalysisExcel")
    public void exportDrvBehaviorAnalysisExcel(NewEnergy vo, HttpServletResponse response) throws Exception {
        SysUserEntity user = (SysUserEntity)request.getSession().getAttribute("SES_CURRENTUSER");
        vo.setCorpId(user.getCorpId());
        List<CommEnerConsStatInfoEntity> list = drvBehaviorAnalysisService.exportDrvBehaviorAnalysis(vo);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        ExportUtil.exportFiles(Arrays.asList(tableHeaderName), Arrays.asList(tablePropertyName), list, response, "excel", 
        		URLEncoder.encode("驾驶行为分析", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "驾驶行为分析");
    }
    
    /**
     * 
     * @param vo
     * @param request
     * @param response
     * @throws Exception
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2014-7-10
     * @功能说明：导出Pdf
     */
    @RequestMapping("/exportDrvBehaviorAnalysisPdf")
    public void exportDrvBehaviorAnalysisPdf(NewEnergy vo, HttpServletResponse response) throws Exception {
        SysUserEntity user = (SysUserEntity)request.getSession().getAttribute("SES_CURRENTUSER");
        vo.setCorpId(user.getCorpId());
        List<CommEnerConsStatInfoEntity> list = drvBehaviorAnalysisService.exportDrvBehaviorAnalysis(vo);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        ExportUtil.exportFiles(Arrays.asList(tableHeaderName), Arrays.asList(tablePropertyName), list, response, "pdf", 
        		URLEncoder.encode("驾驶行为分析", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "驾驶行为分析");
    }
	
    /**
     * 图表分析
     * @param vo
     * @param response
     * @param request
     * @throws Exception
     * @author:zhangy
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/searchLineEnergyChart")
    @ResponseBody
    public Object searchLineEnergyChart(NewEnergy vo) throws Exception {
        SysUserEntity user = (SysUserEntity) request.getSession().getAttribute("SES_CURRENTUSER");
        vo.setCorpId(user.getCorpId());
        List<CommEnerConsStatInfoEntity> list = drvBehaviorAnalysisService.searchLineEnergyChart(vo, request);
        return this.getReturnResult(list);
    }
    
}
